package com.haozhiyan.zhijian.myDao;

import java.util.List;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;
import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.QueryBuilder;

import com.haozhiyan.zhijian.bean.biaoduan.UnitBean;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "UNIT_BEAN".
*/
public class UnitBeanDao extends AbstractDao<UnitBean, String> {

    public static final String TABLENAME = "UNIT_BEAN";

    /**
     * Properties of entity UnitBean.<br/>
     * Can be used for QueryBuilder and for referencing column names.
    */
    public static class Properties {
        public final static Property UnitId = new Property(0, String.class, "unitId", true, "UNIT_ID");
        public final static Property Unit = new Property(1, String.class, "unit", false, "UNIT");
        public final static Property TowerId = new Property(2, String.class, "towerId", false, "TOWER_ID");
    };

    private Query<UnitBean> towerBean_UintChildQuery;

    public UnitBeanDao(DaoConfig config) {
        super(config);
    }
    
    public UnitBeanDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"UNIT_BEAN\" (" + //
                "\"UNIT_ID\" TEXT PRIMARY KEY NOT NULL ," + // 0: unitId
                "\"UNIT\" TEXT," + // 1: unit
                "\"TOWER_ID\" TEXT);"); // 2: towerId
        // Add Indexes
        db.execSQL("CREATE UNIQUE INDEX " + constraint + "IDX_UNIT_BEAN_UNIT_ID ON UNIT_BEAN" +
                " (\"UNIT_ID\" ASC);");
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"UNIT_BEAN\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, UnitBean entity) {
        stmt.clearBindings();
 
        String unitId = entity.getUnitId();
        if (unitId != null) {
            stmt.bindString(1, unitId);
        }
 
        String unit = entity.getUnit();
        if (unit != null) {
            stmt.bindString(2, unit);
        }
 
        String towerId = entity.getTowerId();
        if (towerId != null) {
            stmt.bindString(3, towerId);
        }
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, UnitBean entity) {
        stmt.clearBindings();
 
        String unitId = entity.getUnitId();
        if (unitId != null) {
            stmt.bindString(1, unitId);
        }
 
        String unit = entity.getUnit();
        if (unit != null) {
            stmt.bindString(2, unit);
        }
 
        String towerId = entity.getTowerId();
        if (towerId != null) {
            stmt.bindString(3, towerId);
        }
    }

    @Override
    public String readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0);
    }    

    @Override
    public UnitBean readEntity(Cursor cursor, int offset) {
        UnitBean entity = new UnitBean( //
            cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0), // unitId
            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // unit
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2) // towerId
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, UnitBean entity, int offset) {
        entity.setUnitId(cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0));
        entity.setUnit(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setTowerId(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
     }
    
    @Override
    protected final String updateKeyAfterInsert(UnitBean entity, long rowId) {
        return entity.getUnitId();
    }
    
    @Override
    public String getKey(UnitBean entity) {
        if(entity != null) {
            return entity.getUnitId();
        } else {
            return null;
        }
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "uintChild" to-many relationship of TowerBean. */
    public List<UnitBean> _queryTowerBean_UintChild(String towerId) {
        synchronized (this) {
            if (towerBean_UintChildQuery == null) {
                QueryBuilder<UnitBean> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.TowerId.eq(null));
                towerBean_UintChildQuery = queryBuilder.build();
            }
        }
        Query<UnitBean> query = towerBean_UintChildQuery.forCurrentThread();
        query.setParameter(0, towerId);
        return query.list();
    }

}
